package com.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBSystem {
	public static final String KEY_ROWID = "_ID";
	public static final String SERVERNAMEWIFI = "BM_ServerNameWifi";
	public static final String SERVERPORT = "BM_ServerPort";
	public static final String LOGINSQL = "BM_LoginSQL";
	public static final String PASSWORDSQL = "BM_PasswordSQL";
	public static final String DATABASENAME = "BM_DatabaseName";
	public static final String REMEMBER = "BM_Remember";
	public static final String STATUSBATSO = "BM_Status";
	public static final String CHINHANH = "BM_ChiNhanh";
	public static final String SERVERNAME3G = "BM_ServerName3G";
	public static final String STATUSSERVERNAME = "BM_StatusServerName";
	public static final String USERNAME = "BM_UserName";
	public static final String PASSWORD = "BM_Password";

	public static final String DATABASE_NAME = "BMCALL";
	public static final String DATABASE_TABLE_SYSTEM = "BM_SYSTEM";
	public static final int DATABASE_VERSION = 1;

	private DbHelper kHelper;
	private final Context kContext;
	private SQLiteDatabase kDatabase;

	private static class DbHelper extends SQLiteOpenHelper {

		public DbHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			String query = "create table " + DATABASE_TABLE_SYSTEM + " ("
					+ KEY_ROWID + " integer primary key," + SERVERPORT
					+ " text not null, " + SERVERNAMEWIFI + " text not null,"
					+ LOGINSQL + " text not null," + PASSWORDSQL
					+ " text not null," + DATABASENAME + " text not null,"
					+ REMEMBER + " text not null," + STATUSBATSO
					+ " text not null," + CHINHANH + " text not null,"
					+ SERVERNAME3G + " text not null," + STATUSSERVERNAME
					+ " text not null," + USERNAME + " text not null,"
					+ PASSWORD + " text not null " + "); ";
			db.execSQL(query);

			query = "INSERT INTO " + DATABASE_TABLE_SYSTEM + "(" + KEY_ROWID
					+ "," + SERVERPORT + ", " + SERVERNAMEWIFI + ", "
					+ LOGINSQL + ", " + PASSWORDSQL + ", " + DATABASENAME
					+ ", " + REMEMBER + ", " + STATUSBATSO + ", " + CHINHANH
					+ ", " + SERVERNAME3G + ", " + STATUSSERVERNAME + ", "
					+ USERNAME + ", " + PASSWORD + ")"
					+ "VALUES(1, '', '', '', '', '', '0', '0', '', '', 'wifi', '', '')";
			db.execSQL(query);

		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			db.execSQL("drop table if exists " + DATABASE_TABLE_SYSTEM);
			onCreate(db);
		}
	}

	public DBSystem(Context c) {
		kContext = c;
	}

	public DBSystem open() {
		kHelper = new DbHelper(kContext);
		kDatabase = kHelper.getWritableDatabase();
		return this;
	}

	public void close() {
		kHelper.close();
	}

	public void Insert(String ID, String ServerNameWifi, String ServerPort,
			String Login, String Password, String DatabaseName,
			String Remember, String Connect) {
		ContentValues cv = new ContentValues();
		cv.put(KEY_ROWID, ID);
		cv.put(SERVERNAMEWIFI, ServerNameWifi);
		cv.put(SERVERPORT, ServerPort);
		cv.put(LOGINSQL, Login);
		cv.put(PASSWORDSQL, Password);
		cv.put(DATABASENAME, DatabaseName);
		cv.put(REMEMBER, Remember);
		cv.put(STATUSBATSO, Connect);
		kDatabase.insert(DATABASE_TABLE_SYSTEM, null, cv);
	}

	public void Update(String ServerNameWifi, String ServerPort, String Login,
			String Password, String DatabaseName, String Remember,
			String Connect, String ChiNhanh, String ServerName3G,
			String StatusServerName) {
		ContentValues cv = new ContentValues();
		cv.put(SERVERNAMEWIFI, ServerNameWifi);
		cv.put(SERVERPORT, ServerPort);
		cv.put(LOGINSQL, Login);
		cv.put(PASSWORDSQL, Password);
		cv.put(DATABASENAME, DatabaseName);
		cv.put(REMEMBER, Remember);
		cv.put(STATUSBATSO, Connect);
		cv.put(CHINHANH, ChiNhanh);
		cv.put(SERVERNAME3G, ServerName3G);
		cv.put(STATUSSERVERNAME, StatusServerName);
		kDatabase.update(DATABASE_TABLE_SYSTEM, cv, null, null);
	}

	public void UpdateRemember(String Remember) {
		ContentValues cv = new ContentValues();
		cv.put(REMEMBER, Remember);
		kDatabase.update(DATABASE_TABLE_SYSTEM, cv, null, null);
	}

	public void UpdateStatus(String Status) {
		ContentValues cv = new ContentValues();
		cv.put(STATUSBATSO, Status);
		kDatabase.update(DATABASE_TABLE_SYSTEM, cv, null, null);
	}

	public void UpdateStatusServerName(String StatusServerName) {
		ContentValues cv = new ContentValues();
		cv.put(STATUSSERVERNAME, StatusServerName);
		kDatabase.update(DATABASE_TABLE_SYSTEM, cv, null, null);
	}
	
	public void UpdateUserNamePassword(String strUserName, String strPassword) {
		ContentValues cv = new ContentValues();
		cv.put(USERNAME, strUserName);
		cv.put(PASSWORD, strPassword);
		kDatabase.update(DATABASE_TABLE_SYSTEM, cv, null, null);
	}

	public Cursor SelectSystemAll() {
		return kDatabase.query(DATABASE_TABLE_SYSTEM, new String[] { KEY_ROWID,
				SERVERNAMEWIFI, SERVERPORT, LOGINSQL, PASSWORDSQL,
				DATABASENAME, REMEMBER, STATUSBATSO, CHINHANH, SERVERNAME3G,
				STATUSSERVERNAME, USERNAME, PASSWORD }, null, null, null, null, null);
	}

}
